﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;
using University.Workshop.Model.CardGame;
using System.Globalization;

namespace University.Workshop.Model
{
    public class Table
    {
        //decks of users
        //public Collection<int> DeckOfUser { get; private set; }
        
        public Collection<string> UserTable { get;private set; }
        public Collection<string> Chips { get; private set; }
        public Collection<int> Level {get;private set;}
        public Collection<int> TimeHandsWon { get; private set; }
        public bool IsStart { get; set; }
        public Collection<bool> AllIn { get; private set; }
         [CLSCompliantAttribute(false)]
        public Collection<bool> States { get; private set; }
        public int Users { get; set; }
        public Collection<long> UsersBet { get; private set; }
        public Collection<long> TurnBet { get; private set; }
      
        public Collection< NetworkStream> Partners { get;private set; }
        public Collection<NetworkStream> CardStream { get; private set; }
        
        public bool Status { get;private set; }
        public int Start { get; private set; }
        public int Turn { get; private set; }
        public string LastBet { get; private set; }
        public int Dealer { get; private set; }
        public int ActualCard{get;private set;}

        //public string CardsOfTable { get { return CardsOfTable; } set { CardsOfTable = value; } }
        public Collection<string> CardsOfTable { get;private  set; }
        public Collection<string> CardsOfUser { get; private set; }
        

        private Deck CardList;
        private PokerHand TableCard;
        int ptrTableCard = 0;

        public Table()
        {
            Reset();
           // ResetDeckOfUser();
        }
        public void ResetTurnBet()
        {
            for (int i = 0; i < 5; i++)
            {
                TurnBet[i] = 0;
            }
        }
        public void ResetCardsOfUser()
        {
            CardsOfTable = new Collection<string>();
            CardsOfUser = new Collection<string>();
            LogicalWinner.LogicalWinnerUser.Instance.NumberOfWinner = "1";
            for (int Iterator = 0; Iterator < 5; Iterator++)
            {
                CardsOfUser.Add(null);
                CardsOfUser.Add(null);
            }
        }
        
        public int GetOnlyOneUserInTable
        {
            get
            {
                for (int i = 0; i < Users; i++)
                {
                    if (States[i])
                    {
                        return i;
                    }
                }
                return 0;
            }
        }
        public void DecreaseMyChips()
        {
            int index = 0;
            try
            {
                foreach (long user in TurnBet)
                {
                    Chips[index] = (Convert.ToInt64(Chips[index], CultureInfo.InvariantCulture) - user).ToString(CultureInfo.InvariantCulture);
                    index++;
                }
            }
            catch (FormatException)
            {
            }
        }
        public long GetPrice
        {
            get
            {
                long price = 0;
                foreach (long user in UsersBet)
                {
                    price += user;
                }
                for (int index = 0; index < Users; index++)
                    UsersBet[index] = 0;

                return price;
            }
        }
        public long GetMinimum
        {
            get
            {
                long minimun = (Convert.ToInt64(Chips[0], CultureInfo.InvariantCulture));
                try
                {
                    for (int index = 1; index < Users && !String.IsNullOrEmpty(Chips[index]); index++)
                    {
                        if (States[index])
                        {
                            long value = Convert.ToInt64(Chips[index], CultureInfo.InvariantCulture);
                            if (value < minimun) minimun = value;
                        }
                    }
                }
                catch (FormatException)
                {

                }
                return minimun;
            }
        }
        public void SetLastBet(string value)
        {
            LastBet = value;
        }
        public void SetStart(bool value)
        {
            IsStart = value;
        }
        public void ResetTurn()
        {
            Turn = 0;
        }
        public void NextCard()
        {
            ActualCard++;
        }
        public void Reset()
        {
            IsStart = false;
            CardsOfUser = new Collection<string>();
            CardsOfTable = new Collection<string>();
            //DeckOfUser = new Collection<int>();
            Dealer = 0;
            ActualCard = 0;
            AllIn = new Collection<bool>();
            LastBet = null;
            Turn = 0;
            TurnBet = new Collection<long>();
            UsersBet = new Collection<long>();
            Chips = new Collection<string>();
            UserTable = new Collection<string>();
            Start = 0;
            Users = 0;
            Status = false;
            Partners = new Collection<NetworkStream>();
            CardStream = new Collection<NetworkStream>();
            Level = new Collection<int>();
            TimeHandsWon = new Collection<int>();

            States = new Collection<bool>();
            for (int Iterator = 0; Iterator < 5; Iterator++)
            {
                TurnBet.Add(0);
                AllIn.Add(false);
                UsersBet.Add(0);
                UserTable.Add(null);
                Chips.Add(null);
                States.Add(true);
                CardsOfUser.Add(null);
                Level.Add(0);
                TimeHandsWon.Add(0);
            }

            CardList = new Deck();

            CardList.Shuffle();
            TableCard = new PokerHand(CardList);
            TableCard.PullCards(); //Pull the first five cards
        }
        public void SetState(int index,bool state)
        {
            States[index] = state;
        }
        public void SetStatus(bool newStatus)
        {
            Status = newStatus;
        }
        public string GetStartName
        {
            get{
                return UserTable[Start];
            }
            
        }
        public void ResetCardNumber()
        {
            ActualCard = 0;
            CardList.Shuffle();
        }
        public void Next()
        {

            
            do
            {
                Start++;
                if (Start >= Users)
                    Start = 0;
            } while (States[Start] == false);
            
        }
        public void NextDealer()
        {
            Dealer++;
            if (Dealer >= Users)
                Dealer = 0;
            Start = Dealer;
            LastBet = UserTable[Dealer];
        }
        public bool OnlyOneOnTable()
        {
            int count = 0;
            for (int i = 0; i < Users; i++)
            {
               
                if (States[i] == true)
                    count++;
            }
            return (count == 1);
        }
        public void ResetStates()
        {
            for (int i = 0; i < Users; i++)
            {
                States[i] = true;
            }
        }
        public void NextTurn()
        {
            Turn++;
        }


        //return//One card of 47 - Remember the first five card are the table cards
        public Card PullCard()
        {
            return( CardList.PullCard() );
        }

        //Return one of the five hands card
        public Card PullTableCard()
        {
            Card ReturnCard;
            if (ptrTableCard < 5)
            {
                ReturnCard = TableCard[ptrTableCard];
                ptrTableCard++;
            }
            else
            {
                ReturnCard = TableCard[ptrTableCard];
                ptrTableCard = 0;
            }
            return( ReturnCard );
        }
    }
}
